package com.xbongbong.saas.enums.subform;

import com.xbongbong.paas.enums.FieldTypeEnum;
import lombok.Getter;

import java.util.Objects;

/**
 *
 * @author 魏荣杰
 * @date 2020/8/24 19:10
 * @since
 * @version
 */
@Getter
public enum ContractProductSubFormEnum {
    /**
     *
     */
    BARCODE("barcode", "text_21", FieldTypeEnum.TEXT.getType()),
    CONTRACT_ID("contractId", "num_21", FieldTypeEnum.NUM.getType()),
    CONTRACT_OUTSTOCK_NUM("contractOutstockNum", "num_22", FieldTypeEnum.NUM.getType()),
    COST("cost", "num_23", FieldTypeEnum.NUM.getType()),
    DISCOUNT("discount", "num_24", FieldTypeEnum.NUM.getType()),
    HISTORICAL_PRICE("historicalPrice", "num_25", FieldTypeEnum.NUM.getType()),
    INSTOCK_NUM("instockNum", "num_26", FieldTypeEnum.NUM.getType()),
    IS_PRODUCE("isProduce", "num_27", FieldTypeEnum.NUM.getType()),
    MEMO("memo", "text_22", FieldTypeEnum.TEXT.getType()),
    OUTSTOCK_NUM("outstockNum", "num_28", FieldTypeEnum.NUM.getType()),
    PARENT_ID("parentId", "long_1", FieldTypeEnum.LONG.getType()),
    PRICE("price", "num_30", FieldTypeEnum.NUM.getType()),
    PRODUCT_ID("productId", "long_2", FieldTypeEnum.LONG.getType()),
    PRODUCT_NAME("productName", "text_23", FieldTypeEnum.TEXT.getType()),
    PRODUCT_NO("productNo", "text_24", FieldTypeEnum.TEXT.getType()),
    PRODUCT_NUM("productNum", "num_32", FieldTypeEnum.NUM.getType()),
    PRODUCT_UNIT("productUnit", "text_25", FieldTypeEnum.TEXT.getType()),
    REF_PRODUCT_ID("refProductId", "num_33", FieldTypeEnum.NUM.getType()),
    SORT("sort", "num_34", FieldTypeEnum.NUM.getType()),
    SPECIFICATION("specification", "text_26", FieldTypeEnum.TEXT.getType()),
    SUM_COST("sumCost", "num_35", FieldTypeEnum.NUM.getType()),
    SUM_PRICE("sumPrice", "num_36", FieldTypeEnum.NUM.getType()),
    TYPE("type", "num_37", FieldTypeEnum.NUM.getType()),
    WAIT_OUTSTOCK_NUM("waitOutStockNum", "num_38", FieldTypeEnum.NUM.getType()),
    SHIPMENTS_NUM("shipmentsNum", "num_51", FieldTypeEnum.NUM.getType()),
    STATUS("status","num_39",FieldTypeEnum.NUM.getType()),
    PURCHASE_NUM("purchaseNum","num_40",FieldTypeEnum.NUM.getType()),
    ALREADY_PURCHASE_NUM("alreadyPurchaseNum","num_18",FieldTypeEnum.NUM.getType()),

//    PRODUCT_THUMBNAIL("productThumbnail", "file_1", FieldTypeEnum.IMAGE.getType()),
    ;
    /**
     * 实体attr
     */
    private final String dbAttr;
    /**
     * data内attr
     */
    private final String dataAttr;
    /**
     * 字段类型，详见
     *
     * @see com.xbongbong.paas.enums.FieldTypeEnum
     */
    private final Integer fieldType;

    ContractProductSubFormEnum(String dbAttr, String dataAttr, Integer fieldType) {
        this.dbAttr = dbAttr;
        this.dataAttr = dataAttr;
        this.fieldType = fieldType;
    }


    public static String getAttrConnectData(ContractProductSubFormEnum cache) {
        return "data." + cache.getDataAttr();
    }

    public static String getEsAttr(ContractProductSubFormEnum cache) {
        FieldTypeEnum fieldTypeEnum = FieldTypeEnum.getFieldTypeEnum(cache.getFieldType());
        //字段类型为text，或者字段在es内的类型时text，则需要在后面拼上“.keyword”
        boolean flag = fieldTypeEnum == FieldTypeEnum.TEXT || "text".equals(fieldTypeEnum.getEsalias());
        if (flag) {
            return "data." + cache.getDataAttr() + ".keyword";
        }
        return "data." + cache.getDataAttr();
    }

    public static String getEsAttrByDbAttr(String dbAttr) {
        for (ContractProductSubFormEnum value : values()) {
            if (Objects.equals(dbAttr, value.getDbAttr())) {
                return getEsAttr(value);
            }
        }
        return null;
    }

    public static String getAttrConnectDataByDbAttr(String dbAttr) {
        for (ContractProductSubFormEnum value : values()) {
            if (Objects.equals(dbAttr, value.getDbAttr())) {
                return getAttrConnectData(value);
            }
        }
        return null;
    }
}
